﻿using KFC.ServiceReferenceKFC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KFC.MonAnF
{
    class QuanLyMonAn
    {
        public static List<MonAn> dsMonAn;
        MonAnFactory monAnFactory;
        
        ServiceReferenceKFC.Service1SoapClient client = new ServiceReferenceKFC.Service1SoapClient();

        protected static QuanLyMonAn _instance = null;

        protected QuanLyMonAn()
        {
            monAnFactory = new MonAnFactory();
            monAnFactory.KhoiTaoDSLoaiMonAn();
            monAnFactory.KhoiTaoDanhSachMonAn();
            dsMonAn = monAnFactory.LayDSMonAn();
        }

        public static QuanLyMonAn Instance()
        {
            if (_instance == null)
                _instance = new QuanLyMonAn();
            return _instance;
        }

        public void ThemMon(MonAn monAn)
        {
            try
            {
                dsMonAn.Add(monAn);
                //ServiceReferenceKFC.Service1SoapClient client = new ServiceReferenceKFC.Service1SoapClient();
                SafeMonAn sMA = monAn.GetSafeInfo();
                SafeMonAnDTO sMADTO = sMA.convertToDTO();
                client.ThemMonAn(sMADTO);
                //client.ThemMonAn(monAn.GetSafeInfo().convertToDTO());
            }
            catch (Exception ex)
            {
                return;
            }
            
        }

        public void HuyMon(string maMonAn)
        {
            int idx = -1;
            for (int i = 0; i < dsMonAn.Count; i++)
            {
                if (dsMonAn[i].MaMonAn == maMonAn)
                {
                    idx = i;
                    break;
                }
            }

            if (idx == -1)
                return;
            dsMonAn.Remove(dsMonAn[idx]);
            client.HuyMonAn(maMonAn);
        }

        public void CapNhatGia(string maMon, int gia)
        {
            client.CapNhatGia(maMon, gia);
        }
    }
}
